import { ObjCreate } from "../../common/ObjCreate";
import { ResFileConst } from "../../../resource/ResFileConst";
import { ScrollPolicy } from "../../../../../fl/controls/ScrollPolicy";
import { DropDirectionItemRenderer } from "./itemRender/DropDirectionItemRenderer";
import { LoaderHelp } from "../../../../common/display/LoaderHelp";
import { GlobalClass } from "../../../../../com/mui/core/GlobalClass";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { EventName } from "../../../mvc/EventName";
import { DataEvent } from "../../../events/DataEvent";
import { DisplayUtil } from "../../../../common/DisplayUtil";
import { DirectionCopyConfig } from "../../../resource/DirectionCopyConfig";
import { DropDirectionData } from "./info/DropDirectionData";
import { DataProvider } from "../../../../../fl/data/DataProvider";
import { TDirectionCopy } from "../../../../../Message/Db/Tables/TDirectionCopy";
import { TCopy } from "../../../../../Message/Db/Tables/TCopy";
import { Cache } from "../../../cache/Cache";
import { CopyConfig } from "../../../resource/CopyConfig";
import { MsgManager } from "../../../manager/MsgManager";
import { EDirectionCopyStatus } from "../../../../../Message/Public/EDirectionCopyStatus";
import { KingCopyItemData } from "./info/KingCopyItemData";
import { GLoadedButton } from "../../../../../com/mui/controls/GLoadedButton";
import { GTileList } from "../../../../../com/mui/controls/GTileList";
import { _index } from "../../../../../this/_index";
type int = number;
//class KingCopyItemView
    
    export  class KingCopyItemView /* flash.display.Sprite */
    {
        constructor(arg1: int)
        {
            
            this._index = arg1;
            this.addEventListeners();
            this.init();
            return;
        }

        /* internal  */init(): void
        {
            this.addSprite();
            var loc1=new Object /* flash.display.Sprite */();
            loc1.addEventListener(flash.events.MouseEvent.CLICK, this.onOpenUpHandler);
            this.addChild(loc1);
            this._bg = new Object /* flash.display.Bitmap */();
            ObjCreate.setObjAttri(this._bg, 0, 0, -1, -1, loc1);
            this._stateBitmap = new Object /* flash.display.Bitmap */();
            ObjCreate.setObjAttri(this._stateBitmap, 15, 0, -1, -1);
            this._gradeBitmap = new Object /* flash.display.Bitmap */();
            ObjCreate.setObjAttri(this._gradeBitmap, 390, -2, -1, -1);
            this._openUpBtn = ObjCreate.createGLoadedButton(ResFileConst.KingCopyOpenUpBtn, 460, 5, 32, 33);
            this._openUpBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onOpenUpHandler);
            this._shrinkBtn = ObjCreate.createGLoadedButton(ResFileConst.KingCopyShrinkBtn, 460, 5, 32, 33);
            this._shrinkBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onShrinkHandler);
            this._dropList = ObjCreate.createTileList(28, 36, 500, 120);
            this._dropList.scrollPolicy = ScrollPolicy.OFF;
            this._dropList.columnWidth = 95;
            this._dropList.rowHeight = 120;
            this._dropList.setStyle("cellRenderer", DropDirectionItemRenderer);
            LoaderHelp.addResCallBack(ResFileConst.kingCopy, this.onLoadResHandler);
            return;
        }

        /* internal  */addSprite(): void
        {
            var loc1=new Object /* flash.display.Sprite */();
            loc1.graphics.drawRect(0, -2, 516, 50);
            this.addChild(loc1);
            return;
        }

        /* internal  */onLoadResHandler(): void
        {
            var loc1=GlobalClass.getBitmap(this.COPY_NAME + this._index);
            ObjCreate.setObjAttri(loc1, (516 - loc1.width) / 2, 10, -1, -1, this);
            this.addChild(this._stateBitmap);
            return;
        }

        /* internal  */addEventListeners(): void
        {
            Dispatcher.addEventListener(EventName.ClickKingCopyItemOpenOrShrinkBtn, this.onOpenOrShrinkBtnHandler);
            Dispatcher.addEventListener(EventName.ShowKingCopyDropList, this.showDropList);
            return;
        }

        /* internal  */showDropList(arg1: DataEvent): void
        {
            this._dropList.visible = true;
            return;
        }

        /* internal  */onOpenOrShrinkBtnHandler(arg1: DataEvent): void
        {
            var loc1=0;
            var loc2=null;
            var loc3=null;
            var loc4=null;
            var loc5=0;
            if (this.isCanOpen) 
            {
                loc1 = arg1.data.index;
                if (loc1 != this._index) 
                {
                    DisplayUtil.removeMe(this._dropList);
                    this.controlBtnShow = false;
                }
                else 
                {
                    loc2 = [];
                    if (loc4 = DirectionCopyConfig.instance.getItemByFloor(loc1 + 1)) 
                    {
                        loc5 = 0;
                        while (loc5 < 5) 
                        {
                            (loc3 = new DropDirectionData()).copyCode = loc4.copyCode;
                            loc3.index = loc5 + 1;
                            loc2.push(loc3);
                            ++loc5;
                        }
                        this._dropList.width = 500;
                        this._dropList.height = 120;
                        this._dropList.dataProvider = new DataProvider(loc2);
                        this._dropList.drawNow();
                        this._dropList.visible = false;
                        this.controlBtnShow = true;
                        this.addChild(this._dropList);
                    }
                }
            }
            return;
        }

        /* internal  */onOpenUpHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var e: Object /* flash.events.MouseEvent */;
            var updateKingCopyItemList: Function;
            var level: int;
            var combat: int;
            var directionDrop: TDirectionCopy;
            var copyInfo: TCopy;
            var copyLevel: int;

            var loc1;
            level = 0;
            combat = 0;
            directionDrop = null;
            copyInfo = null;
            copyLevel = 0;
            e = arg1;
            if (this.isCanOpen) 
            {
                updateKingCopyItemList = (): void
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.UpdateKingCopyItemList));
                    return;
                }
                Dispatcher.dispatchEvent(new DataEvent(EventName.ClickKingCopyItemOpenOrShrinkBtn, {"index": _index}));
                flash.utils.setTimeout(updateKingCopyItemList, 100);
            }
            else 
            {
                level = Cache.instance.role.roleInfo.level;
                combat = int(Cache.instance.role.combatCapabilities);
                directionDrop = DirectionCopyConfig.instance.getItemByFloor(this._index + 1);
                if (directionDrop) 
                {
                    copyInfo = CopyConfig.instance.getInfoByCode(directionDrop.copyCode);
                    if (copyInfo) 
                    {
                        copyLevel = copyInfo.enterMinLevel;
                    }
                    if (level < copyLevel) 
                    {
                        MsgManager.showRollTipsMsg("等级不足，需要等级:" + copyLevel + "级");
                        return;
                    }
                    if (combat < directionDrop.requireCombat) 
                    {
                        MsgManager.showRollTipsMsg("战力不足,需要战力:" + directionDrop.requireCombat);
                        return;
                    }
                }
            }
            return;
        }

        /* internal  */get isCanOpen(): Boolean
        {
            return this._data && !(this._data.copyState == EDirectionCopyStatus._EDirectionCopyNotOpen);
        }

        /* internal  */onShrinkHandler(arg1: Object /* flash.events.MouseEvent */=null): void
        {
            this._dropList.dataProvider = new DataProvider([]);
            DisplayUtil.removeMe(this._dropList);
            Dispatcher.dispatchEvent(new DataEvent(EventName.UpdateKingCopyItemList));
            this.controlBtnShow = false;
            return;
        }

        /* internal  */set controlBtnShow(arg1: Boolean)
        {
            if (arg1) 
            {
                this.addChild(this._shrinkBtn);
                DisplayUtil.removeMe(this._openUpBtn);
            }
            else 
            {
                this.addChild(this._openUpBtn);
                DisplayUtil.removeMe(this._shrinkBtn);
            }
            return;
        }

        public set data(arg1: KingCopyItemData)
        {
            this._data = arg1;
            if (this._data) 
            {
                this._stateBitmap.bitmapData = GlobalClass.getBitmapData(this.STATE_NAME + this._data.copyState);
                if (this._data.copyGrade > 0) 
                {
                    this._gradeBitmap.bitmapData = GlobalClass.getBitmapData(KingCopyItemView.LEVEL_NAME + this._data.copyGrade);
                }
                if (this._data.todayFinishCopysIndex != this._index) 
                {
                    this.controlBtnShow = false;
                }
                else 
                {
                    this.controlBtnShow = true;
                }
            }
            return;
        }

        /* internal */ /* const */COPY_NAME: string="KingCopy_";

        /* internal */ /* const */STATE_NAME: string="KingCopyState_";

        public static /* const */LEVEL_NAME: string="KingCopyGradeLevel_";

        /* internal */ /* var */_bg: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_stateBitmap: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_gradeBitmap: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_openUpBtn: GLoadedButton;

        /* internal */ /* var */_shrinkBtn: GLoadedButton;

        /* internal */ /* var */_dropList: GTileList;

        /* internal */ /* var */_data: KingCopyItemData;

        /* internal */ /* var */_index: int;
    }
